package com.tz.springmvc.sysmanage.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.tz.springmvc.sysmanage.entity.User;
import com.tz.springmvc.sysmanage.vo.UserVo;

/**
 * 用注解的方式开发用户的增删改查功能
 * @author Administrator
 *
 */
@Controller
//@RequestMapping("/sysmgr/user")
public class UserController{

	//查询用户列表
	@RequestMapping("/getUserList")
	public ModelAndView getUserList(){
		System.out.println("进入getUserList");
		ModelAndView modelAndView  = new ModelAndView();
		List<User> userList = new ArrayList<User>();
 		User user1 = new User();
 		User user2 = new User();
 		User user3 = new User();
 		user1.setUserId(1l);user1.setUsername("huihunfeng");user1.setSex("girl");user1.setAge(18);
 		user2.setUserId(2l);user2.setUsername("mac");user2.setSex("girl");user2.setAge(18);
 		user3.setUserId(3l);user3.setUsername("liangtian");user3.setSex("girl");user3.setAge(18);
 	
 		userList.add(user1);userList.add(user2);userList.add(user3);
 		//将数据放入modelAndView对象
 		modelAndView.addObject("userList",userList);
 		//讲返回的逻辑名称放入modelAndView对象
 		modelAndView.setViewName("userList");
 		
		return modelAndView;
	}
	
	
	
	/*public void handleRequest(HttpServletRequest request, HttpServletResponse response,HttpSession httpSession) 			
			throws ServletException, IOException {
 		System.out.println("进入UserContorller");
 		List<User> userList = new ArrayList<User>();
 		User user1 = new User(1l,"huihunfeng",18,"girl");
 		User user2 = new User(2l,"mac",18,"boy");
 		User user3 = new User(3l,"qide",18,"boy");
 		userList.add(user1);userList.add(user2);userList.add(user3);	
 		request.setAttribute("userList", userList);
 		request.getRequestDispatcher("/userList.jsp").forward(request,response);
	}*/
	
	//进入用户管理页面
	@RequestMapping("/gotoUser")
	public String gotoUser(){
		return "userMain";		
	}
	//进入用户新增或者修改页面
	@RequestMapping("/gotoUserEdit")
	public String gotoUserEdit(){
		return "userEdit";		
	}
	
	//保存用户新增或者修改页面的信息,用user对象获取参数
	@RequestMapping("/saveUser")
	public String saveUser(User user){
		
		System.out.println("user.getUsername()="+user.getUsername());
		System.out.println("user.getSex()="+user.getSex());
		System.out.println("user.getAge()="+user.getAge());
		return "userEdit";		
	}
	
	//通过包装类来获取页面信息
	@RequestMapping("/saveUserVo")
	public String saveUserVo(UserVo userVo){	
		System.out.println("user.getUsername()="+userVo.getUser().getUsername());
		System.out.println("user.getSex()="+userVo.getUser().getSex());
		System.out.println("user.getAge()="+userVo.getUser().getAge());
		return "userEdit";		
	}
	
	//根据用户id数组批量删除用户	
	@RequestMapping("/batchDelUser")
	public String batchDelUser(Integer[] userId){		
		if(userId.length>0){
			for(int i = 0;i<userId.length;i++){
				System.out.println("userId["+i+"]="+userId[i]);

			}
		}		
		return "userList";		
	}

	//根据包装类来获取批量修改的用户信息
	@RequestMapping("/batchUpdateUser")
	public String batchUpdateUser(UserVo userVo){		
		System.out.println("userVo.getUserList().size()="+userVo.getUserList().size());
		
		for(int i = 0;i<userVo.getUserList().size();i++){
			System.out.println("userVo.getUserList().get(i).getUsername()="+
											userVo.getUserList().get(i).getUsername());
		}
		return "userList";		
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
